package com.ehotting.mqbd.zw.api.domain.netty;


import com.ehotting.eaf.core.utils.ByteUtils;
import com.ehotting.eaf.netty.domain.Request;
import com.ehotting.mqbd.zw.api.constants.ActionConstants;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;


public class ZwCommPacket extends Request {

    private ZwCommPacketHead head = new ZwCommPacketHead();
    private byte[] buff;

    public ZwCommPacket() {
    }

    public ZwCommPacket(String action) {
        super(action);
        ActionConstants actionConstant = ActionConstants.getByActionName(action);
        this.head.setCmd(actionConstant.getZwCommandVal());
        this.head.setPeerPackId(Integer.parseInt(this.serial));
    }

    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public ZwCommPacket (@JsonProperty("action") String action,
                         @JsonProperty("head") ZwCommPacketHead head,
                         @JsonProperty("buff") byte[] buff){
        super(action);
        this.head = head;
        this.buff = buff;
    }

    public ZwCommPacket(String action, String serial) {
        super(action, serial);
    }

    public ZwCommPacketHead getHead() {
        return head;
    }

    public void setHead(ZwCommPacketHead head) {
        this.head = head;
    }

    public byte[] getBuff() {
        return buff;
    }

    public void setBuff(byte[] buff) {
        this.buff = buff;
    }

    /**
     * 将request中的属性转换为16进制字符串
     * 以和->中望
     */
    public void convertToHexStr() {

    }

    /**
     * 将16进制字符串转换为request的属性
     * 中望->以和
     */
    public void convertToProp() {

    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        byte[] buff = this.getBuff();
        byte[] peerIdBytes = ByteUtils.intToBytesL(head.getPeerId());
        buf.append(ByteUtils.byteToHex(peerIdBytes));
        byte[] peerPackIdBytes = ByteUtils.intToBytesL(head.getPeerPackId());
        buf.append(ByteUtils.byteToHex(peerPackIdBytes));
        byte[] buffSizeBytes;
        if (buff==null) {
            // 如果buff字段为null，则长度为0
            buffSizeBytes = ByteUtils.intToBytesL(0);
        }else {
            buffSizeBytes = ByteUtils.intToBytesL(buff.length);
        }
        buf.append(ByteUtils.byteToHex(buffSizeBytes));
        byte[] cmdBytes = ByteUtils.shortToBytesL(head.getCmd());
        buf.append(ByteUtils.byteToHex(cmdBytes));

        buf.append(ByteUtils.byteToHex(new byte[]{head.getPackProp()}));
        buf.append(ByteUtils.byteToHex(new byte[]{head.getCmdProp()}));

        if (buff!=null) {
            buf.append(ByteUtils.byteToHex(buff));
        }

        return buf.toString();
    }
}
